﻿using System;
using System.Linq;
using BCCommon;
using BCEntity.OA.PunchEdits;
using BCData.Common.OA.PunchEdits;
using BCDto.MySql.Admin;
using BCDto.OA.PunchEdits;
using BCService.Utils;

namespace BCService.OA.PunchEdits
{
    /// <summary>
    /// 考勤打卡信息
    /// </summary>
    public class PunchEditsService : IPunchEditsService
    {
        private readonly IPunchEditsData punchEditsData;

        public PunchEditsService(IPunchEditsData punchEditsData)
        {
            this.punchEditsData = punchEditsData;
        }

        /// <summary>
        /// 创建打卡记录
        /// </summary>
        /// <param name="adminDto"></param>
        /// <param name="punchEditsRequestDto"></param>
        /// <returns></returns>
        public PunchEditsDto Create(AdminDto adminDto, PunchEditsRequestDto punchEditsRequestDto)
        {
            if (adminDto == null)
            {
                throw new ArgumentException("登录超时，请重新登录");
            }

            if (punchEditsRequestDto == null)
            {
                throw new ArgumentException("打卡信息不能为空");
            }
            PunchEditsEntity punchEditsEntity = punchEditsRequestDto.As<PunchEditsEntity>();
            punchEditsEntity.PunchEditsNo = Guid.NewGuid().ToString("N");
            punchEditsEntity.AdminId = adminDto.AdminId;
            var result = this.punchEditsData.Create(punchEditsEntity);
            if (result == null)
            {
                return null;
            }
            return result.As<PunchEditsDto>();
        }

        /// <summary>
        /// 删除一条打卡记录
        /// </summary>
        /// <param name="punchEditsNo"></param>
        /// <returns></returns>
        public int Delete(string punchEditsNo)
        {
            if (!this.punchEditsData.Exists(punchEditsNo))
            {
                throw new ArgumentException("打卡信息不存在");
            }
            return this.punchEditsData.Delete(punchEditsNo);
        }

        /// <summary>
        /// 获取指定一条打卡记录
        /// </summary>
        /// <param name="punchEditsNo"></param>
        /// <returns></returns>
        public PunchEditsDto Get(string punchEditsNo)
        {
            return this.punchEditsData.Get(punchEditsNo).As<PunchEditsDto>();
        }

        /// <summary>
        /// 分页获取打卡记录列表
        /// </summary>
        /// <param name="adminFullName">员工名称</param>
        /// <param name="idCard"></param>
        /// <param name="phone"></param>
        /// <param name="adminId"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="ascending"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public PageableList<PunchEditsInfoDto> List(string adminFullName, string idCard, string phone, long? adminId, DateTime? start, DateTime? end, bool? ascending, int pageIndex, int pageSize)
        {
            var (list, count) = this.punchEditsData.List(adminFullName, idCard, phone, adminId, start, end, ascending, pageIndex, pageSize);

            return new PageableList<PunchEditsInfoDto>
            {
                Count = count,
                Items = list.Select(p => p.As<PunchEditsInfoDto>())
            };
        }

        /// <summary>
        /// 修改打卡信息
        /// </summary>
        /// <param name="punchEditsNo"></param>
        /// <param name="punchEditsSimPutDto"></param>
        /// <returns></returns>
        public PunchEditsDto Update(string punchEditsNo, PunchEditsSimPutDto punchEditsSimPutDto)
        {
            if (punchEditsSimPutDto == null)
            {
                throw new ArgumentException("修改的打卡信息不能为空");
            }

            var punchEditsEntity = punchEditsData.Get(punchEditsNo);
            if (punchEditsEntity == null)
            {
                throw new ArgumentException("打卡信息不存在");
            }
            punchEditsEntity.OutsideWork = punchEditsSimPutDto.OutsideWork;
            punchEditsEntity.Latitude = punchEditsSimPutDto.Latitude;
            punchEditsEntity.Longitude = punchEditsSimPutDto.Longitude;
            punchEditsEntity.OutsideWorkCoordinate = punchEditsSimPutDto.OutsideWorkCoordinate;
            punchEditsEntity.Remark = punchEditsSimPutDto.Remark;
            return this.punchEditsData.Update(punchEditsEntity).As<PunchEditsDto>();
        }
    }
}
